package vip.liux.contracts.repositories.pay;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.ListCrudRepository;
import org.springframework.data.repository.ListPagingAndSortingRepository;
import org.springframework.data.repository.NoRepositoryBean;
import vip.liux.contracts.models.pay.PayOrder;
import vip.liux.contracts.shared.enums.pay.PayOrderStatus;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;
import java.util.Optional;

@NoRepositoryBean
public interface PayOrderRepository extends ListPagingAndSortingRepository<PayOrder, Long>, ListCrudRepository<PayOrder, Long> {

    Optional<PayOrder> findByAppIdAndMerchantOrderId(Long appId, String merchantOrderId);

    Long countByAppId(Long appId);

    List<PayOrder> findByIdIn(Collection<Long> ids);

    Page<PayOrder> findPageBy(Long appId, String channelCode, String merchantOrderId, String orderNo, String channelOrderNo, PayOrderStatus status, LocalDateTime start, LocalDateTime end, Pageable page);

    List<PayOrder> findListBy(Long appId, String channelCode, String merchantOrderId, String orderNo, String channelOrderNo, PayOrderStatus status, LocalDateTime start, LocalDateTime end);

    int updateByIdAndStatus(String orderNo, PayOrderStatus status, BigDecimal refundPrice);

    void updateByIdSetPrice(Long id, BigDecimal payPrice);

    List<PayOrder> selectListByStatusAndExpireTimeLt(PayOrderStatus status, LocalDateTime expireTime);

    Optional<PayOrder> findByOrderNo(String orderNo);

    int updateOrderSuccess(Long id, String orderNo, PayOrderStatus currentStatus, PayOrderStatus updateStatus, Long channelId, String channelCode, LocalDateTime successTime, String channelOrderNo, String channelUserId, Double channelFeeRate, BigDecimal channelFeePrice, String json);

    int updateOrderClosed(Long id, String orderNo, Long channelId, String channelCode, PayOrderStatus currentStatus, PayOrderStatus updateStatus, String channelNotifyData, String channelErrorCode, String channelErrorMsg);

    List<PayOrder> findByIdAndStatus(Long id, PayOrderStatus payOrderStatus);

    List<PayOrder> findByStatusAndCreationTimeGreaterThan(PayOrderStatus status, LocalDateTime minCreationTime);
}